package com.hlf.modules.api.controller;

import com.hlf.common.annotation.SysLog;
import com.hlf.common.constants.Constants;
import com.hlf.common.utils.IdWorkerFactory;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.api.annotation.LoginUser;
import com.hlf.modules.api.interceptor.AuthorizationInterceptor;
import com.hlf.modules.merchant.goods.entity.Goods;
import com.hlf.modules.merchant.goods.entity.GoodsType;
import com.hlf.modules.merchant.goods.service.GoodsService;
import com.hlf.modules.merchant.goods.service.GoodsTypeService;
import com.hlf.modules.sys.controller.AbstractController;
import com.hlf.modules.sys.entity.File;
import com.hlf.modules.sys.entity.SysUserEntity;
import com.hlf.modules.sys.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品管理接口
 */
@RestController
@RequestMapping("/api/goods")
@Api("商品管理接口")
public class ApiGoodsController extends AbstractController{

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsTypeService goodsTypeService;
    @Autowired
    private FileService fileService;




    /**
     * 查询单个
     */
    @GetMapping("obj/{goodsId}")
    @ApiOperation(value = "单个商品", notes = "单个商品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "goodsId", value = "商品id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult goodsObj(@PathVariable("goodsId") String goodsId) {
        Assert.isBlank(goodsId, "商品id不能为空");
        long goodsIdL = Long.parseLong(goodsId);
        Goods goods = goodsService.queryObject(goodsIdL);
        List<File> fileList = fileService.selectByForeignId(goodsIdL);
        goods.setGoodsImg(fileList);
        Map<String, Object> map = new HashMap<>();
        map.put(Constants.DEFAULT_RESULT_NAME, goods);
        return ReturnResult.ok(map);
    }



    /**
     * 根据商品类型分类后的 商品列表
     */
    @GetMapping("list")
    @ApiOperation(value = "根据商品类型分类后的 商品列表", notes = "根据商品类型分类后的 商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult list(@LoginUser SysUserEntity user) {

        List<GoodsType> goodsTypes = goodsTypeService.batchQueryListStoreId(Long.parseLong(user.getCurrentStoreId()));
        Map<String, Object> map = new HashMap<>();
        map.put(Constants.DEFAULT_RESULT_NAME, goodsTypes);
        return ReturnResult.ok(map);
    }

    /**
     * 售罄/上下架/修改茶位费
     * 状态和茶位费必须有一个
     */
    @SysLog("售罄/上下架/修改茶位费")
    @PostMapping("updateStatus")
    @ApiOperation(value = "售罄/上下架/修改茶位费", notes = "售罄/上下架/修改茶位费")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "goodsId", value = "商品id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "status", value = "状态 0:下架  1:上架  2:售罄", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "teaPri", value = "茶位费价格", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult updateGoodsStatus(String goodsId, String status, HttpServletRequest request, String teaPri) {
        Assert.isBlank(goodsId, "商品id不能为空");
        logger.warn("操作的商品id{}"+goodsId);
        goodsService.updateGoodsStatus(goodsId, status, request, teaPri);
        return ReturnResult.ok();
    }


    /******************************************************商品类型******************************************************/
    /**
     * 新建商品类型
     */
    @SysLog("新建商品类型")
    @PostMapping("createType")
    @ApiOperation(value = "新建商品类型", notes = "新建商品类型")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "typeName", value = "商品类型名称", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "printerId", value = "打印机id", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult createGoodsType(String typeName, String printerId, @LoginUser SysUserEntity user) {
        Assert.isBlank(typeName, "商品类型名称不能为空");

        Long userId = user.getUserId();
        GoodsType goodsType = new GoodsType();
        goodsType.setId(IdWorkerFactory.nextId());
        goodsType.setStoreId(Long.parseLong(user.getCurrentStoreId()));
        goodsType.setInsertBy(userId);
        goodsType.setName(typeName);
        goodsType.setPrinterId(printerId == null ? 0L : Long.parseLong(printerId));
        goodsTypeService.insertSelective(goodsType);

        return ReturnResult.ok();
    }

    /**
     * 查询单个商品类型
     */
    @GetMapping("objType/{goodsTypeId}")
    @ApiOperation(value = "单个商品类型", notes = "单个商品类型")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "goodsTypeId", value = "商品类型id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult goodsTypeObj(@PathVariable("goodsTypeId") String goodsTypeId) {
        Assert.isBlank(goodsTypeId, "商品类型id不能为空");

        GoodsType goodsType = goodsTypeService.queryObject(Long.parseLong(goodsTypeId));
        Map<String, Object> map = new HashMap<>();
        map.put(Constants.DEFAULT_RESULT_NAME, goodsType);
        return ReturnResult.ok(map);
    }


    /**
     * 修改商品类型
     */
    @SysLog("修改商品类型")
    @PostMapping("updateType")
    @ApiOperation(value = "修改商品类型", notes = "修改商品类型")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "goodsTypeId", value = "商品类型id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "typeName", value = "商品类型名称", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "printerId", value = "打印机id", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult updateGoodsType(String typeName, String goodsTypeId, String printerId) {
        Assert.isBlank(typeName, "商品类型名称不能为空");
        Assert.isBlank(goodsTypeId, "商品类型id不能为空");

        GoodsType goodsType = new GoodsType();
        goodsType.setId(Long.parseLong(goodsTypeId));
        goodsType.setName(typeName);
        if (printerId != null && !printerId.isEmpty()) {
            goodsType.setPrinterId(Long.parseLong(printerId));
        }
        goodsTypeService.updateByPrimaryKeySelective(goodsType);
        return ReturnResult.ok();
    }

    /**
     * 商品类型列表
     */
    @GetMapping("listType")
    @ApiOperation(value = "商品类型列表", notes = "商品类型列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult goodsTypeList(@LoginUser SysUserEntity user) {

        List<GoodsType> goodsTypes = goodsTypeService.queryListForStoreId(user.getCurrentStoreId());
        Map<String, Object> map = new HashMap<>();
        map.put(Constants.DEFAULT_RESULT_NAME, goodsTypes);
        return ReturnResult.ok(map);
    }


    /**
     * 批量逻辑删除商品类型
     */
    @SysLog("批量删除商品类型")
    @PostMapping("delType")
    @ApiOperation(value = "批量逻辑删除商品类型", notes = "批量逻辑删除商品类型")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Long", name = "goodsTypeIdList", value = "商品类型id/可多个", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult batchDeleteType(HttpServletRequest request, @RequestParam(value = "goodsTypeIdList", required = true) List<Long> goodsTypeIdList) {
        Assert.isNull(goodsTypeIdList, "商品类型id不能为空");
        logger.info("批量删除的商品类型id{}"+goodsTypeIdList);
        Long userId = (Long) request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY);
        int i = goodsTypeService.batchUpdateDel(goodsTypeIdList, userId);
        if (i == 0) {
            return ReturnResult.error("删除失败!");
        }
        return ReturnResult.ok();
    }
}